
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[ClubSeasons_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[ClubSeasons_Insert]

IF OBJECT_ID(N'[dbo].[ClubSeasons_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[ClubSeasons_Update]

IF OBJECT_ID(N'[dbo].[ClubSeasons_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[ClubSeasons_Delete]

IF OBJECT_ID(N'[dbo].[ClubSeasons_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[ClubSeasons_Select]

--endregion

GO


--region [dbo].[ClubSeasons_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ClubSeasons_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdClubSeasons int = null,
	@IdClub int = null,
	@IdSeason int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[ClubSeasons]
					WHERE
					(
						(@IdClubSeasons is null OR [ClubSeasons].[IdClubSeasons] = @IdClubSeasons)
						AND (@IdClub is null OR [ClubSeasons].[IdClub] = @IdClub)
						AND (@IdSeason is null OR [ClubSeasons].[IdSeason] = @IdSeason)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[ClubSeasons].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdClubSeasons' and @SortType = 1 THEN [ClubSeasons].[IdClubSeasons] END ASC,
						CASE WHEN @SortBy = 'IdClubSeasons' and @SortType = 0 THEN [ClubSeasons].[IdClubSeasons] END DESC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 1 THEN [ClubSeasons].[IdClub] END ASC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 0 THEN [ClubSeasons].[IdClub] END DESC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 1 THEN [ClubSeasons].[IdSeason] END ASC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 0 THEN [ClubSeasons].[IdSeason] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[ClubSeasons]
			Where 
			(
				(@IdClubSeasons is null OR [ClubSeasons].[IdClubSeasons] = @IdClubSeasons)
				AND (@IdClub is null OR [ClubSeasons].[IdClub] = @IdClub)
				AND (@IdSeason is null OR [ClubSeasons].[IdSeason] = @IdSeason)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[ClubSeasons_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[ClubSeasons_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ClubSeasons_Insert]
	@IdClubSeasons int OUTPUT,
	@IdClub int,
	@IdSeason int

AS


INSERT INTO [dbo].[ClubSeasons] 
(
	[IdClub],
	[IdSeason]
)
VALUES 
(
	@IdClub,
	@IdSeason
)

SET @IdClubSeasons = SCOPE_IDENTITY()

--end [dbo].[ClubSeasons_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[ClubSeasons_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ClubSeasons_Update]
	@IdClubSeasons int,
	@IdClub int,
	@IdSeason int
AS


UPDATE [dbo].[ClubSeasons] SET
	[IdClub] = @IdClub,
	[IdSeason] = @IdSeason
WHERE
	[IdClubSeasons] = @IdClubSeasons

--end [dbo].[ClubSeasons_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[ClubSeasons_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ClubSeasons_Delete]
	@IdClubSeasons int
AS


DELETE FROM [dbo].[ClubSeasons]
WHERE
(
	[IdClubSeasons] = @IdClubSeasons
)

--end [dbo].[ClubSeasons_Delete]
--endregion

GO
--=========================================================================================--

